/***************************************************************************
 * 文件名：FMAddReference
 * 功能：
 * 说明   添加引用界面
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/14 15:08:08
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.Utils.Extensions;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Internal;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace BuiltInTools
{
	public partial class FMAddReference : XtraForm
	{
		#region Propertys

		private string _refType;
		private string _solutionPath;
		private List<CSProjectModel> _projects;
		private string _centralPkgPropsPath;

		/// <summary>
		/// 中央包筛选提示
		/// </summary>
		public string CentralPkgFilter = "输入以筛选包";

		/// <summary>
		/// 项目筛选
		/// </summary>
		public string ProjFilter = "输入以筛选项目";

		/// <summary>
		/// 中央包文件名
		/// </summary>
		private string _CentralPackageFileName;

		/// <summary>
		/// 指示当前页面正在通过中央包管理创建包引用
		/// </summary>
		public bool IsCreatingPkgRefByCentralPkg
		{
			get { return _refType == ReferenceTypeConst.PkgRef && File.Exists(this._CentralPackageFileName); }
		}

		/// <summary>
		/// 可操作的项目列表
		/// </summary>
		private List<CSProjectModel> OptionProjList = new List<CSProjectModel>();

		/// <summary>
		/// 解决方案项目列表
		/// </summary>
		private List<SolutionProject> _SolutionProjects { get; set; } = new List<SolutionProject>();

		/// <summary>
		/// CS包展示列表
		/// </summary>
		private List<CSPackage> CSPackages = new List<CSPackage>();

		/// <summary>
		/// 中央包模型类
		/// </summary>
		private CentralPackage _CentralPackage { get; set; }

		/// <summary>
		/// 项目展示列表
		/// </summary>
		private List<CSProjectModel> DisplayProjects = new List<CSProjectModel>();

		/// <summary>
		/// 程序集引用展示列表
		/// </summary>
		private List<AssemblyRefModel> DisplayAssemblyRefenences = new List<AssemblyRefModel>();

		/// <summary>
		/// 项目引用展示列表
		/// </summary>
		private List<ProjectRefModel> DisplayProjectReferences = new List<ProjectRefModel>();

		/// <summary>
		/// 项目引用列表
		/// </summary>
		private List<ProjectRefModel> ProjectReferences { get; set; } = new List<ProjectRefModel>();

		#region ListView绑定数据源

		/// <summary>
		/// 右侧ListView引用展示列表
		/// </summary>
		private List<CSProjectReferenceItemModel> DisplayReferenceList = new List<CSProjectReferenceItemModel>();

		/// <summary>
		/// 程序集引用列表展示数据（最终）
		/// </summary>
		private List<ListViewItemModel> AssemblyListViewItemModels = new List<ListViewItemModel>();

		/// <summary>
		/// 项目引用列表展示数据（最终）
		/// </summary>
		private List<ListViewItemModel> ProjectListViewItemModels = new List<ListViewItemModel>();

		#endregion

		#endregion

		#region Win

		public FMAddReference(string solutionPath, List<CSProjectModel> projects, string refType, string centralPkgPropsPath)
		{
			InitializeComponent();

			_solutionPath = solutionPath;
			_projects = projects;
			_refType = refType;
			_centralPkgPropsPath = centralPkgPropsPath;

			#region 赋值

			if (projects?.Count > 0)
			{
				OptionProjList.AddRange(projects);
			}

			///中央包管理器名称
			string defaultcentralPkgPropsPath = Path.Combine(Path.GetDirectoryName(solutionPath), "Directory.Packages.props");
			if ((string.IsNullOrEmpty(centralPkgPropsPath) || !File.Exists(centralPkgPropsPath)) && File.Exists(defaultcentralPkgPropsPath))
			{
				_CentralPackageFileName = defaultcentralPkgPropsPath;
			}
			else
			{
				_CentralPackageFileName = centralPkgPropsPath;
			}


			#endregion
		}

		/// <summary>
		/// Load事件处理程序
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FMAddReference_Load(object sender, System.EventArgs e)
		{
			#region 初始化数据

			//加载数据
			LoadReferenceData();

			//初始化引用类型下拉框
			InitComboxValue();

			//动态添加StackPanelControl控件
			InitStackPanelControl();

			//初始化ListBoxControl控件
			InitListBoxControl();

			#endregion
		}

		#endregion

		#region Methods

		/// <summary>
		/// 加载数据
		/// </summary>
		private void LoadReferenceData()
		{
			try
			{
				if (File.Exists(_solutionPath))
				{
					_SolutionProjects = SolutionUtil.GetSolutionProjectList(_solutionPath);
				}

				//检测传递过来的是否为空，若为空则从解决方案项目中拉取
				if (OptionProjList?.Count == 0)
				{
					_SolutionProjects.ForEach(data => OptionProjList.Add(CSProjectModel.CreateDefault(data.ProjectName, data.RelativePath, data.FullName)));
				}

				if (File.Exists(_CentralPackageFileName))
				{
					//清空数据
					CSPackages.Clear();

					//添加包引用列表
					_CentralPackage = CentralPackageUtil.LoadFile(_CentralPackageFileName);
					CSPackages.AddRange(_CentralPackage.CustomPackages);
					CSPackages.AddRange(_CentralPackage.OfficalPackages);
					CSPackages.AddRange(_CentralPackage.OtherPackages);

					//构建界面数据展示项目引用列表
					ProjectReferences.Clear();
					ProjectReferences.AddRange(OptionProjList.Select(p => new ProjectRefModel
					{
						ReferenceName = p.ProjectName,
						AbsolutePath = p.FileName,
					}));
					DisplayProjectReferences.AddRange(ProjectReferences);
				}

				//添加可选项目列表数据至展示列表
				DisplayProjects.AddRange(OptionProjList);

				//刷新数据
				RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 刷新界面数据
		/// </summary>
		private void RefreshData()
		{
			try
			{
				//清空listView绑定数据源
				AssemblyListViewItemModels.Clear();
				ProjectListViewItemModels.Clear();

				///解决方案引用列表
				switch (_refType)
				{
					case ReferenceTypeConst.Ref:
						{
							if (DisplayAssemblyRefenences?.Count > 0)
							{
								AssemblyListViewItemModels.AddRange(DisplayAssemblyRefenences.Select(p => new ListViewItemModel
								{
									ShowText = p.ReferenceName,
									Name = File.Exists(p.FileName) ? p.FileName : p.ReferenceName,
									Tag = File.Exists(p.FileName) ? p.FileName : p.ReferenceName,
								}));
							}
							break;
						}
					case ReferenceTypeConst.ProjRef:
						{
							if (DisplayProjectReferences?.Count > 0)
							{
								AssemblyListViewItemModels.AddRange(DisplayProjectReferences.Select(p => new ListViewItemModel
								{
									ShowText = p.ReferenceName,
									Name = p.ReferenceName,
									Tag = p.AbsolutePath,
								}));
							}
							break;
						}
					case ReferenceTypeConst.PkgRef:
					default:
						{
							if (CSPackages?.Count > 0)
							{
								AssemblyListViewItemModels.AddRange(CSPackages.Select(p => new ListViewItemModel
								{
									ShowText = p.PackageName,
									Name = p.PackageName,
									Tag = p.PackageVersion,
								}));
							}
							break;
						}
				}

				listBoxControl1.DataSource = null;
				listBoxControl1.DataSource = AssemblyListViewItemModels;

				///项目引用列表
				if (DisplayProjects?.Count > 0)
				{
					ProjectListViewItemModels.AddRange(DisplayProjects.Select(p => new ListViewItemModel
					{
						ShowText = p.ProjectName,
						Name = p.FileName,
						Tag = p.RelativePath
					}));
				}

				listBoxControl2.DataSource = null;
				listBoxControl2.DataSource = ProjectListViewItemModels;

				//绑定右侧listView ,展示选择的数据
				var list = new List<CSProjectReferenceItemModel>(DisplayReferenceList.OrderBy(p => p.ProjectName));
				DisplayReferenceList.Clear();
				DisplayReferenceList.AddRange(list);
				gridControl1.DataSource = DisplayReferenceList;
				gridControl1.MainView.RefreshData();

				ApplyEnable();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 仅控制界面元素的可用性
		/// </summary>
		private void ApplyEnable()
		{
			this.Text = $"添加{(_refType == ReferenceTypeConst.Ref ? "程序集" : (_refType == ReferenceTypeConst.ProjRef ? "项目" : "包"))}引用-" + _solutionPath;

			groupControl1.Text = IsCreatingPkgRefByCentralPkg ? "中央包" : ("引用：" + ReferenceTypeConst.GetDescription(_refType));


			panelControl2.Visible = panelControl4.Visible = _refType != ReferenceTypeConst.ProjRef;
		}

		/// <summary>
		/// 加载引用类型下拉框数据
		/// </summary>
		private void InitComboxValue()
		{
			try
			{
				cbxSelectRefType.Properties.Items.Add("程序集引用");
				cbxSelectRefType.Properties.Items.Add("项目引用");
				cbxSelectRefType.Properties.Items.Add("NuGet包引用");

				//避免初次加载时触发SelectedValueChanged事件
				cbxSelectRefType.SelectedValueChanged -= cbxSelectRefType_SelectedValueChanged;
				switch (_refType)
				{
					case ReferenceTypeConst.ProjRef:
						{
							cbxSelectRefType.SelectedIndex = 1;
							break;
						}
					case ReferenceTypeConst.PkgRef:
						{
							cbxSelectRefType.SelectedIndex = 2;
							break;
						}
					case ReferenceTypeConst.Ref:
					default:
						{
							cbxSelectRefType.SelectedIndex = 0;
							break;
						}
				}

				cbxSelectRefType.SelectedValueChanged += cbxSelectRefType_SelectedValueChanged;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 动态添加StackPanel控件
		/// </summary>
		private void InitStackPanelControl()
		{
			StackPanelControl stackPanel1 = new StackPanelControl();
			stackPanel1.Dock = DockStyle.Fill;
			stackPanel1.AutoSize = true;
			stackPanel1.ContentOrientation = Orientation.Horizontal;
			stackPanel1.ContentAlignment = DevExpress.Utils.HorzAlignment.Center;

			//添加简单按钮
			SimpleButton simpleButton1 = new SimpleButton()
			{
				Text = "+",
				Size = new Size(80, 80),
				Font = new Font("微软雅黑", 20F, FontStyle.Regular,GraphicsUnit.Point, ((byte)(134))),
			};
			stackPanel1.AddControl(simpleButton1);
			panelControl3.AddControl(stackPanel1);


			StackPanelControl stackPanel2 = new StackPanelControl();
			stackPanel2.Dock = DockStyle.Fill;
			stackPanel2.AutoSize = true;
			stackPanel2.ContentOrientation = Orientation.Horizontal;
			stackPanel2.ContentAlignment = DevExpress.Utils.HorzAlignment.Center;

			//添加简单按钮
			SimpleButton simpleButton2 = new SimpleButton()
			{
				Text = "->",
				Size = new Size(80, 80),
				Font = new Font("微软雅黑", 20F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)))
			};

			//绑定事件
			simpleButton2.Click += Transfer_Click;

			stackPanel2.AddControl(simpleButton2);
			panelControl5.AddControl(stackPanel2);
		}

		/// <summary>
		/// 初始化ListBoxControl控件
		/// </summary>
		private void InitListBoxControl()
		{
			try
			{
				//设置左侧ListBoxControl控件
				listBoxControl1.DataSource = AssemblyListViewItemModels;
				listBoxControl1.DisplayMember = "ShowText";
				listBoxControl1.ValueMember = "Name";
				listBoxControl1.SelectedIndex = -1;

				//设置右侧ListBoxControl控件
				listBoxControl2.DataSource = ProjectListViewItemModels;
				listBoxControl2.DisplayMember = "ShowText";
				listBoxControl2.ValueMember = "Name";
				listBoxControl2.SelectedIndex = -1;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region Event

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <exception cref="NotImplementedException"></exception>
		private void Transfer_Click(object sender, EventArgs e)
		{
			try
			{

				if (listBoxControl1.SelectedItems?.Count == 0 || listBoxControl2.SelectedItems?.Count == 0)
				{
					return;
				}

				var listViewProjItemList = new List<ListViewItemModel>();

				foreach (ListViewItemModel data in listBoxControl1.SelectedItems)
				{
					if (data == null) { continue; }
					listViewProjItemList.Add(data);
				}

				var listViewRefItemList = new List<ListViewItemModel>();

				foreach (ListViewItemModel data in listBoxControl2.SelectedItems)
				{
					if (data == null) { continue; }
					listViewRefItemList.Add(data);
				}

				var insertRefList = new List<CSProjectReferenceItemModel>();

				listViewRefItemList.ForEach(p =>
				{
					insertRefList.AddRange(listViewProjItemList
						.Where(x => !DisplayReferenceList.Any(z => z.ProjectName == x.ShowText && z.ReferenceName == p.ShowText))
						.Select(x => new CSProjectReferenceItemModel
						{
							ProjectName = x.ShowText,
							ProjectFileName = p.Name,
							ReferenceType = _refType,
							ReferenceName = p.ShowText,
							ReferenceValue = x.Tag,
						}));
				});

				insertRefList.ForEach(p =>
				{
					var projItem = DisplayReferenceList.LastOrDefault(x => x.ProjectName == p.ProjectName);
					if (projItem != null)
					{
						DisplayReferenceList.Insert(DisplayReferenceList.IndexOf(projItem) + 1, p);
					}
					else
					{
						DisplayReferenceList.Add(p);
					}
				});

				//刷新数据
				bsData.DataSource = DisplayReferenceList;
				gridControl1.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 右键菜单事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
		{
			// 检查是否是右键点击
			if (e.HitInfo.InRowCell)
			{
				// 显示您的 ContextMenuStrip
				contextMenuStrip1.Show(Cursor.Position);
			}
		}

		/// <summary>
		/// 删除选中数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = gridControl1.MainView as GridView;

				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<CSProjectReferenceItemModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as CSProjectReferenceItemModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}

				selectRows.ForEach(data=> DisplayReferenceList.Remove(data));

				// 刷新数据
				bsData.DataSource = DisplayReferenceList;
				gridControl1.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 双击事件删除数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			try
			{
				var gridView = gridControl1.MainView as GridView;

				if (gridView == null)
				{
					return;
				}

				var selectedRows = gridView.GetFocusedRow() as CSProjectReferenceItemModel;
				if(selectedRows == null)
				{
					return;
				}

				DisplayReferenceList.Remove(selectedRows);

				// 刷新数据
				bsData.DataSource = DisplayReferenceList;
				gridControl1.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 查抄引用文本框内容变化事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtSearchReference_TextChanged(object sender, EventArgs e)
		{
			try
			{
				CSPackages.Clear();
				AssemblyListViewItemModels.Clear();
				CSPackages.AddRange(_CentralPackage.CustomPackages.Where(p => p.PackageName.ToLower().Contains(txtSearchReference.Text.ToLower())));
				CSPackages.AddRange(_CentralPackage.OfficalPackages.Where(p => p.PackageName.ToLower().Contains(txtSearchReference.Text.ToLower())));
				CSPackages.AddRange(_CentralPackage.OtherPackages.Where(p => p.PackageName.ToLower().Contains(txtSearchReference.Text.ToLower())));

				CSPackages.ForEach(data => AssemblyListViewItemModels.Add(new ListViewItemModel
				{
					Name = data.PackageName,
					ShowText = data.PackageName,
					Tag = data.PackageVersion,
				}));

				//刷新数据
				listBoxControl1.DataSource = AssemblyListViewItemModels;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 添加引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddReference_Click(object sender, EventArgs e)
		{
			try
			{
				switch (_refType)
				{
					case ReferenceTypeConst.Ref:
						{
							FMAddAssemblyReference form = new FMAddAssemblyReference();
						
							DialogResult dialogResult = form.ShowDialog();
							if(dialogResult!= DialogResult.OK)
							{
								return;
							}

							DisplayAssemblyRefenences.Add(new AssemblyRefModel()
							{
								ReferenceName = form.AssemblyName,
								FileName = form.HintPath,
							});
							break;
						}

					case ReferenceTypeConst.PkgRef:
						{
							FMAddCentralPackage form = new FMAddCentralPackage(IsCreatingPkgRefByCentralPkg);
							// 显示模态窗口
							DialogResult dialogResult = form.ShowDialog();
							if(dialogResult!= DialogResult.OK)
							{
								return;
							}

							CSPackage newPkg = new CSPackage();
							if (IsCreatingPkgRefByCentralPkg)
							{
								newPkg = new CentralPackageUtil(_CentralPackage).AddPackage(form.PackageName, form.PackageVersion, form.PackageType);
							}
							else
							{
								newPkg.PackageName = form.PackageName;
								newPkg.PackageVersion = form.PackageVersion;
							}
							CSPackages.Add(newPkg);
							break;
						}
					default:
						break;
				}

				//刷新数据
				RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 删除引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteReference_Click(object sender, EventArgs e)
		{
			try
			{

				if (listBoxControl1.SelectedItems?.Count == 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var listViewProjItemList = new List<ListViewItemModel>();

				foreach (ListViewItemModel data in listBoxControl1.SelectedItems)
				{
					if (data == null) { continue; }
					listViewProjItemList.Add(data);
				}

				if (listViewProjItemList?.Count == 0)
				{
					return;
				}

				string msg = IsCreatingPkgRefByCentralPkg ? "是否确认删除选中的中央包？" : $"是否移除选中的{(_refType == ReferenceTypeConst.Ref ? "程序集" : "包")}引用？";
				if (MessageBox.Show(msg, "是否确认", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) != DialogResult.Yes)
				{
					return;
				}

				//查找中央包引用
				var deletePkgList = CSPackages
					.Where(p => listViewProjItemList.Any(x => x.Name == p.PackageName))
					.ToList();

				//移除中央包中的引用
				deletePkgList.ForEach(pkg =>
				{
					CSPackages.Remove(pkg);
					pkg.Element.Remove();
				});

				//删除程序集引用中选中的数据
				listViewProjItemList.ForEach(item => { AssemblyListViewItemModels.Remove(item); });

				listBoxControl1.DataSource = AssemblyListViewItemModels;

				//保存
				CentralPackageUtil.Save(_CentralPackage);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 查找可选项目
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtSearchProject_TextChanged(object sender, EventArgs e)
		{
			try
			{
				DisplayProjects.Clear();
				ProjectListViewItemModels.Clear();
				DisplayProjects.AddRange(this.OptionProjList.Where(p => p.ProjectName.ToLower().Contains(txtSearchProject.Text.ToLower())));
				if (DisplayProjects?.Count <= 0)
				{
					return;
				}
				DisplayProjects.ForEach(data => ProjectListViewItemModels.Add(new ListViewItemModel
				{
					ShowText = data.ProjectName,
					Name = data.FileName,
					Tag = data.RelativePath,
				}));

				listBoxControl2.DataSource = ProjectListViewItemModels;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 保存引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSaveReference_Click(object sender, EventArgs e)
		{
			try
			{
				foreach (var proj in DisplayReferenceList.Where(p => !string.IsNullOrWhiteSpace(p.ReferenceName)).ToList())
				{
					ReferenceEditor.InsertReference(proj.ProjectFileName, _refType, proj.ReferenceValue);
				}

				var list = new List<CSProjectReferenceItemModel>(DisplayReferenceList.Where(p => !string.IsNullOrWhiteSpace(p.ReferenceName))).ToList();

				if (list?.Count > 0)
				{
					foreach (var item in list)
					{
						DisplayReferenceList.Remove(item);
					}
				}

				MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				this.DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 切换引用类型下拉框选中值变化事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cbxSelectRefType_SelectedValueChanged(object sender, EventArgs e)
		{
			try
			{
				string msg = $"切换引用类型将清空表格数据，是否确认继续？";

				if (DisplayReferenceList.Any() && MessageBox.Show(msg, "是否确认", MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
				{
					return;
				}

				DisplayReferenceList.Clear();

				var selectItem = cbxSelectRefType.SelectedItem?.ToString();
				if (string.IsNullOrEmpty(selectItem))
				{
					return;
				}

				var list = ReferenceTypeConst.GetList().Where(p => p.Value.Equals(selectItem));
				if (list?.Count() == 0)
				{
					return;
				}

				_refType = list.First().Key;

				RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

	}
}
